package graffeo.osu;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class gameActivity extends Activity {
	public ImageButton upButton, downButton, leftButton, rightButton;
	public Button teleButton, restartButton, directions;
	public PlayField theField;
	public Player thePlayer = new Player();
	public TextView text;
	public TextView teleports;
	public int numRobots = 0;
	
	private SharedPreferences preferences;
	private GridView field;
	private AlertDialog alert;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent theIntent = getIntent();
		numRobots = theIntent.getIntExtra("r", numRobots);
		if(numRobots == 0){
			getStartingRobots();
		}
		setContentView(R.layout.game);
		teleports = (TextView) findViewById(R.id.teles);
		field = (GridView) findViewById(R.id.grid);
		
		theField = new PlayField(numRobots, this, field);
		theField.placePlayer(thePlayer);
		teleports.setText("Teleports: " + theField.getTeleports());
		initialize();

		upButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				theField.movePlayer("up", thePlayer);
				theField.moveRobot(thePlayer);
				checkWin();
			}
		});
		downButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				theField.movePlayer("down", thePlayer);
				theField.moveRobot(thePlayer);
	//			text.setText(theField.drawPlayField());
				checkWin();
			}
		});
		leftButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				theField.movePlayer("left", thePlayer);
				theField.moveRobot(thePlayer);
				checkWin();
			}
		});
		rightButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				theField.movePlayer("right", thePlayer);
				theField.moveRobot(thePlayer);
				checkWin();
			}
		});
		restartButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startNewGame();
			}
		});
		directions.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Start next activity
				Intent myIntent = new Intent(gameActivity.this,
						directionsActivity.class);
				gameActivity.this.startActivity(myIntent);
			}
		});
		teleButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (Integer.parseInt(theField.getTeleports()) == 0) {
					Toast msg = Toast.makeText(gameActivity.this,
							"Out of teleports!", Toast.LENGTH_SHORT);
					msg.show();
				}
				theField.movePlayer("t", thePlayer);
				theField.moveRobot(thePlayer);
				teleports.setText("Teleports: " + theField.getTeleports());
				checkWin();
			}
		});

	}
	
	public void initialize() {
		upButton = (ImageButton) findViewById(R.id.up);
		downButton = (ImageButton) findViewById(R.id.down);
		leftButton = (ImageButton) findViewById(R.id.left);
		rightButton = (ImageButton) findViewById(R.id.right);
		teleButton = (Button) findViewById(R.id.teleport);
		restartButton = (Button) findViewById(R.id.restartGame);
		directions = (Button) findViewById(R.id.directions);
	}

	// ASK IF THEY WANT TO START A NEW GAME
	public void checkWin() {
		if (thePlayer.getIsDead() == true) {
			alert = new AlertDialog.Builder(this).create();
			alert.setTitle("Game Over");
			alert.setMessage("You Lost.  Play Again?");
			alert.setButton("Yes", new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					startNewGame();
				}
			});

			alert.setButton2("No", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					alert.dismiss();
				}
			});

			alert.show();
		} else if (theField.getWinToken() == true) {
			alert = new AlertDialog.Builder(this).create();
			alert.setTitle("Game Over");
			alert.setMessage("You Won.  Play Again?");
			alert.setButton("Yes", new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					nextGameDifficulty();
				}
			});

			alert.setButton2("No", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					alert.dismiss();
				}
			});

			alert.show();
		}

	}

	public int getStartingRobots() {
		preferences = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());
		String difficulty = preferences.getString("difficulty", "3");
		numRobots = Integer.parseInt(difficulty);
		return numRobots;
	}

	public void startNewGame() {
		preferences = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());
		String difficulty = preferences.getString("difficulty", "3");
		numRobots = Integer.parseInt(difficulty);
		Intent currentIntent = getIntent();
		currentIntent.putExtra("r", numRobots);
		finish();
		startActivity(currentIntent);
		return;
	}

	public void nextGameDifficulty() {
		numRobots++;
		Intent currentIntent = getIntent();
		currentIntent.putExtra("r", numRobots);
		finish();
		startActivity(currentIntent);
		return;
	}

	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		// TODO Auto-generated method stub

		MenuInflater blowUp = getMenuInflater();
		blowUp.inflate(R.menu.options_menu, menu);
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {

		case R.id.aboutUs:
			Intent intentAbout = new Intent("graffeo.osu.ABOUT");
			startActivity(intentAbout);
			break;

		case R.id.preferences:
			Intent intentPreferences = new Intent("graffeo.osu.PREFERENCES");
			startActivity(intentPreferences);
			break;
		}

		return false;
	}

}